﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Chat.Client
{
    public class MtClientExEventArgs<Ex> : EventArgs where Ex : Exception
    {
        public Ex EventException { get; protected set; }

        public MtClientExEventArgs(Ex eventException)
        {
            EventException = eventException;
        }
    }

    /*
    public class MtClient : BaseClient
    {
        protected Thread ThCmdReader { get; set; }

        public event EventHandler<MtClientExEventArgs<Exception>> Disconnected;
        public event
            EventHandler<MtClientExEventArgs<UnknownCmdException>> UnknownCmdReceived;

        public event
            EventHandler<MtClientExEventArgs<UnexpectedCmdException>> UnexpectedCmdReceived;

        public MtClient(
            LoginData loginData, Connection connection, CmdSerializer serializer) :
            base(loginData, connection, serializer)
        {
            ThCmdReader = new Thread(CommandReaderThread);
            ThCmdReader.IsBackground = true;
            ThCmdReader.Start();
        }

        private readonly object closeLock = new object();
        public override void Close()
        {
            lock (closeLock)
            {
                if (Thread.CurrentThread.ManagedThreadId != ThCmdReader.ManagedThreadId)
                {
                    if (ThCmdReader.IsAlive)
                        ThCmdReader.Abort();

                    ThCmdReader = null;
                }

                base.Close();
            }
        }

        protected void CommandReaderThread()
        {
            try
            {
                ProcessServerCommands();
            }
            catch (ThreadAbortException)
            {
            }
        }

        private void ProcessServerCommands()
        {
            Exception disconnectedEx;

            while (true)
            {
                try
                {
                    while (true)
                        ProcessCommand();
                }
                catch (UnknownCmdException ex)
                {
                    OnIllegalCmdReceived(
                        new MtClientExEventArgs<UnknownCmdException>(ex));
                }
                catch (UnexpectedCmdException ex)
                {
                    OnUnexpectedCmdReceived(
                        new MtClientExEventArgs<UnexpectedCmdException>(ex));
                }
                catch (Exception ex) // any other exception qualifed as disconnected
                {
                    disconnectedEx = ex;
                    break;
                }
            }

            OnDisconnected(new MtClientExEventArgs<Exception>(disconnectedEx));
        }

        protected virtual void OnDisconnected(MtClientExEventArgs<Exception> e)
        {
            if (Disconnected != null)
                Disconnected(this, e);
        }

        protected virtual void OnIllegalCmdReceived(
            MtClientExEventArgs<UnknownCmdException> e)
        {
            if (UnknownCmdReceived != null)
                UnknownCmdReceived(this, e);
        }

        protected virtual void OnUnexpectedCmdReceived(
            MtClientExEventArgs<UnexpectedCmdException> e)
        {
            if (UnexpectedCmdReceived != null)
                UnexpectedCmdReceived(this, e);
        }
    }
     * */
}
